**Housekeeping
clear all
cap log close
log using ${rep_root}/logs/explore_complaints.log, text replace
set more off

**Load raw accusation data
import delimited using ${rep_root}/data/FOIA/FOIA_complaints_21-060-216_04282021/clear_accused.csv, bindquote(strict) maxquotedrows(1000)

**Rename record ID number to standardize with other files
rename log_no cr_id
drop if missing(cr_id)

**Add assignment date
merge m:1 cr_id using ${rep_root}/data/inv, keep(2 3) gen(inv_merge) keepusing(inv_assigned_dt)
gen yr = year(inv_assigned_dt)
gen m = month(inv_assigned_dt)

**Add accusation category
merge m:1 allegation_category_cd using ${rep_root}/data/acc_cat, nogen
drop if missing(acc_cat)

**Define time before and after McDonald scandal
gen post = yr>=2016 | (yr==2015 & m==12)
replace post = . if !inrange(yr, 2013, 2018)

**Accusation-level analysis
preserve

keep if !missing(post)

**Note increase in failure to provide service
tab allegation_category_desc post if acc_cat==1, column

**Count complaints per month
sort yr m acc_cat
by yr m acc_cat: gen ym_ct = _N
by yr m acc_cat: keep if _n==1

**Reshape
keep yr m acc_cat ym_ct post
reshape wide ym_ct, i(yr m) j(acc_cat)

**Make stacked variables
gen cum_1 = ym_ct4
gen cum_2 = cum_1 + ym_ct3
gen cum_3 = cum_2 + ym_ct1
gen cum_4 = cum_3 + ym_ct2
gen cum_5 = cum_4 + ym_ct0

gen t = yr + (m-1)/12

**Plot
twoway ///
(area cum_5 cum_4 cum_3 cum_2 cum_1 t, mcolor(%30)), ///
legend(order(5 "Force" 4 "Civil Rights" 3 "Departmental" 2 "Criminal" 1 "Other")) ///
xline(2015.92)
graph export ${rep_root}/output/figures/complaints_stacked_accusation.pdf, replace


restore

**Complaint-level analysis
keep if !missing(post)

sort cr_id accusation_id -acc_cat
by cr_id accusation_id: keep if _n==1

**Note increase in failure to provide service
tab allegation_category_desc post if acc_cat==1, column

**Count complaints per month
sort yr m acc_cat
by yr m acc_cat: gen ym_ct = _N
by yr m acc_cat: keep if _n==1

**Reshape
keep yr m acc_cat ym_ct post
reshape wide ym_ct, i(yr m) j(acc_cat)

**Make stacked variables
gen cum_1 = ym_ct4
gen cum_2 = cum_1 + ym_ct3
gen cum_3 = cum_2 + ym_ct1
gen cum_4 = cum_3 + ym_ct2
gen cum_5 = cum_4 + ym_ct0

gen t = yr + (m-1)/12

**Plot
twoway ///
(area cum_5 cum_4 cum_3 cum_2 cum_1 t), ///
legend(order(5 "Force" 4 "Civil Rights" 3 "Departmental" 2 "Criminal" 1 "Other")) ///
xline(2015.92) xlabel(2013(1)2019)
graph export ${rep_root}/output/figures/complaints_stacked_complaint.pdf, replace
clear

**Sustainments
use  ${rep_root}/data/complaints_2021, replace

**Define time before and after McDonald scandal
gen yr = year(inv_assigned_dt)
gen m = month(inv_assigned_dt)
gen post = yr>=2016 | (yr==2015 & m==12)
replace post = . if !inrange(yr, 2013, 2018)

keep if inlist(acc_cat, 3, 4)

tab acc_sustained post, column chi2
tab acc_susp_days post if acc_sustained==1, column

gen dismissed = acc_sustained==1 & acc_susp_days==9999
tab dismissed post, column chi2
clear

**Information
insheet using "${rep_root}/data//FOIA/CLEAR INFO Status Data.csv" 

sort cr_number
by cr_number: keep if _n==1

gen incident_dt = date(incident_date, "MDY", 2025)
gen incident_yr = year(incident_dt) 
gen incident_m = month(incident_dt)
**Print information count by year
tab incident_yr incident_m if inrange(incident_yr, 2013, 2018)
